#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "linkedlist.h"
#include "fun.h"

//为结点开辟一块空间
GROUP * alloc_one_node()
{
        GROUP * load = NULL;
	
	load = malloc(sizeof(GROUP));
	assert(load != NULL);	
	load->prev = NULL;
	load->next = NULL;
	
	return load;
}

//为分组创建空链表
GROUP * create_linked()
{
	GROUP * head = NULL;
	GROUP * new = NULL, * tail = NULL;

	head = alloc_one_node();
	head->h = NULL;
	tail = head;
	new = alloc_one_node();
	strcpy(new->groupName, "weimingmin");
	new->h = creat_people_list();
	new->prev = tail;
	tail->next = new;
	tail = new;	

	return head;
}

//定位到链表的最后一个结点
GROUP * linked_end(GROUP * head)
{
	GROUP * load = head;

	while(load->next != NULL)
	{
		load = load->next;
	}

	return load;
}

//判断新的分组名是否已存在
int isSameG(GROUP * head, char * name)
{
	GROUP * load = head;

	while(load->next != NULL)
	{
		load = load->next;
		if(strcmp(load->groupName, name) == 0)
		{
			return 0;
		}
	}

	return 1;
}

//添加一个分组
GROUP * add_new_group(GROUP * head, char * name)
{
	GROUP * new = NULL;
	GROUP * load = linked_end(head);

	new = alloc_one_node();
	//去重
	if(isSameG(head, name) == 1)
	{
		strcpy(new->groupName, name);
		new->h = creat_people_list();
		new->prev = load;
		load->next = new;
		return new;
	}
	else
	{
		free(new);
	}

	return load;
}

//打印一个分组
void print_one_group(GROUP * load)
{
	printf("\t%s\n", load->groupName);
}

//打印所有分组
void print_all_group(GROUP * head)
{
	GROUP * load = head;

	while(load->next != NULL)
	{
		load = load->next;
		print_one_group(load);	
	}
}

//查找指定分组
GROUP * sel_one_group(GROUP * head, char * str)
{
	GROUP * load = head;
	
	while(load->next != NULL)
	{
		load = load->next;
		if(strcmp(load->groupName, str) == 0)
		{
			return load;
		}
	}

	return NULL;
}

//删除一个分组
GROUP * del_one_group(GROUP * pload)
{
	GROUP * pre_load = NULL;

	pre_load = pload->prev;
	pre_load->next = pload->next;
	free(pload);

	return pload;
}

//输入Y或N
char isSure()
{
	char ch;

	while(1)
	{
		fflush(stdin);
		ch = getchar();
		if(ch == 'y' || ch == 'Y' || ch == 'n' || ch == 'N')
		{
			break;
		}
	}
	
	return ch;
}

//获取分组个数
int get_group_length(GROUP * load)
{
	int len = 0;

	while(load->next != NULL)
	{
		load = load->next;
		len++;
	}

	return len;
}

//排序分组
GROUP * bubble_sort_group(GROUP * head, int len)
{
	char str[20];
	GROUP * pload = NULL;
	GROUP * load = NULL;
	LXR * t;
	int i;

	for(i = 0; i < len - 1; i++)
	{
		pload = head->next;
		load = pload->next;
		while(load != NULL)
		{
			if(strcmp(pload->groupName, load->groupName) > 0)
			{
				strcpy(str, pload->groupName);
				strcpy(pload->groupName, load->groupName);
				strcpy(load->groupName, str);

				t = pload->h;
				pload->h = load->h;
				load->h = t;
			}
			pload = load;
			load = load->next;
		}		
	}
	
	return head;
}